<?php
/**
* 前端入口
**/
header("content-type:text/html;charset=utf-8");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers:jwtToken");
//error_reporting(E_ALL);
//ini_set('display_errors','On');

function getScriptUrl()
{
  $scriptUrl = '';
    $scriptFile = $_SERVER['SCRIPT_FILENAME'];
    $scriptName = basename($scriptFile);
    if (isset($_SERVER['SCRIPT_NAME']) && basename($_SERVER['SCRIPT_NAME']) === $scriptName) {
        $scriptUrl = $_SERVER['SCRIPT_NAME'];
    } elseif (isset($_SERVER['PHP_SELF']) && basename($_SERVER['PHP_SELF']) === $scriptName) {
        $scriptUrl = $_SERVER['PHP_SELF'];
    } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $scriptName) {
        $scriptUrl = $_SERVER['ORIG_SCRIPT_NAME'];
    } elseif (isset($_SERVER['PHP_SELF']) && ($pos = strpos($_SERVER['PHP_SELF'], '/' . $scriptName)) !== false) {
        $scriptUrl = substr($_SERVER['SCRIPT_NAME'], 0, $pos) . '/' . $scriptName;
    } elseif (!empty($_SERVER['DOCUMENT_ROOT']) && strpos($scriptFile, $_SERVER['DOCUMENT_ROOT']) === 0) {
        $scriptUrl = str_replace([$_SERVER['DOCUMENT_ROOT'], '\\'], ['', '/'], $scriptFile);
    } else {
        die('Unable to determine the entry script URL.');
    }
    return $scriptUrl;
}

function resolveBaseRequestUri()
{
    if (isset($_SERVER['REQUEST_URI'])) {
        $requestUri = $_SERVER['REQUEST_URI'];
        if ($requestUri !== '' && $requestUri[0] !== '/') {
            $requestUri = preg_replace('/^(http|https):\/\/[^\/]+/i', '', $requestUri);
        }
    } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0 CGI
        $requestUri = $_SERVER['ORIG_PATH_INFO'];
    } else {
        die('Unable to determine the request URI.');
    }

    return strpos($requestUri, '?') === false ? $requestUri : strstr($requestUri, '?', true);
}

function getProjectPathPrefixAndWebroot(){
    $scriptUrl = getScriptUrl();
    $requestUri = resolveBaseRequestUri();
    if(strpos($requestUri,$scriptUrl) === false){
        $route = trim(substr($requestUri, strlen(str_replace('\\', '/', dirname($scriptUrl)))),'/');
    }
    else{
        $route = substr($requestUri, strlen($scriptUrl));
    }
    $webroot = $projectPathPrefix = strpos($route, '/') === false ? $route : strstr(trim($route,'/'), '/', true);
    if(is_dir(BASE_PATH . '/apps/' . $projectPathPrefix)){
        return [$projectPathPrefix,$webroot];
    }
    $projectPathPrefix = str_replace('-', '/', $projectPathPrefix);
    if(is_dir(BASE_PATH . '/apps/' . $projectPathPrefix)){
        return [$projectPathPrefix,$webroot];
    }
    return false;
}

defined('YII_DEBUG') or define('YII_DEBUG', false);
defined('YII_ENV') or define('YII_ENV', 'prod');

defined('BASE_PATH') or define('BASE_PATH', dirname(__DIR__));

$projectPathPrefix = '';
if($projectPathInfo = getProjectPathPrefixAndWebroot()){
    list($projectPathPrefix, $webroot) = $projectPathInfo;
    $_SERVER['REQUEST_URI'] = str_replace('/' . trim($webroot,'/') . '/', '/', $_SERVER['REQUEST_URI']);
}
defined('APP_BASE_PATH') or define('APP_BASE_PATH', BASE_PATH . '/apps/' . $projectPathPrefix);

require BASE_PATH . '/vendor/autoload.php';
require BASE_PATH . '/vendor/yiisoft/yii2/Yii.php';
require BASE_PATH . '/common/config/bootstrap.php';
require APP_BASE_PATH . '/config/bootstrap.php';

$config = yii\helpers\ArrayHelper::merge(
    require BASE_PATH . '/common/config/main.php',
    require BASE_PATH . '/common/config/main-local.php',
    require APP_BASE_PATH . '/config/main.php',
    require APP_BASE_PATH . '/config/main-local.php'
);

(new heihei\web\Application($config))->run();